﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyThongTinNhanKhau.model.DAO
{
    class TamTruDAO
    {
        SQLServiceDAO service = new SQLServiceDAO();


        internal bool isValidMaTamTru(string str)
        {
            String sql = "Select TOP(1) * from TamTru where MaTamTru = '" + str + "'";
            DataTable dt = service.getDataTable(sql);

            if (dt.Rows.Count != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        internal DataTable loadDefault(string maNhanKhau)
        {
            String sql = "select * from NHANKHAU where MaNhanKhau = '" + maNhanKhau + "'";
            DataTable dt = service.getDataTable(sql);
            return dt;
        }

        internal DataTable loadTable(string maNhanKhau)
        {
            String sql = "select MaTamTru, t.MaNhanKhau, HoTen, GioiTinh, CMND, NoiDangKiTamTru, NgayTamTru, Case When t.TinhTrang = 'Enable' then N'Mở' Else N'Đóng' End as TinhTrang from TAMTRU t join NHANKHAU n on t.MaNhanKhau = n.MaNhanKhau where t.MaNhanKhau = '"+maNhanKhau+"'";
            DataTable dt = service.getDataTable(sql);
            return dt;
        }

        internal bool themMoiTamTru(string maTamTru, string maNhanKhau, string ngayTamTru, string noiDangKiTamTru, string tinhTrang)
        {
            String sql = "insert into TAMTRU values ('"+maTamTru+"','"+maNhanKhau+"','"+ngayTamTru+"',N'"+noiDangKiTamTru+"', '"+tinhTrang+"')";
            return service.executeNonQuery(sql);
        }

        internal bool capNhatTamTru(string maTamTru, string maNhanKhau, string ngayTamTru, string noiDangKiTamTru, string tinhTrang)
        {
            String sql = "update TAMTRU set NgayTamTru = '" + ngayTamTru + "', NoiDangKiTamTru = N'" + noiDangKiTamTru + "', TinhTrang = N'" + tinhTrang + "' where MaTamTru = N'" + maTamTru + "' and MaNhanKhau = N'" + maNhanKhau + "'";
            return service.executeNonQuery(sql);
        }

        internal bool deleteRow(string maTamTru, string maNhanKhau)
        {
            String sql = "delete from TamTru where MaTamTru = N'" + maTamTru + "' and MaNhanKhau = N'" + maNhanKhau + "'";
            return service.executeNonQuery(sql);
        }
    }
}
